using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace DynamicDNS.Provider.TCP
{
    /// <summary>
    /// Implementation for the TCP-based NOIP Dynamic DNS Provider
    /// </summary>
    public class NOIPProvider : DynamicDNS.Provider.BaseProvider.DynamicDNSTCPBaseProvider,IDynamicDNSProvider
    {
        /// <summary>
        /// This template contains the basic provider specific request.
        /// </summary>
        private const string REQUESTTEMPLATE = "GET /nic/update?" +
            "hostname={0}" +
            "&myip={1}" +
            " HTTP/1.1\r\n" +
            "Host: dynupdate.no-ip.com\r\n" +
            "Authorization: Basic {2}" +
            "\r\n" +
            "User-Agent: DynamicDNS Library\r\n\r\n";

        public NOIPProvider()
        {
            ProviderDomain = "dynupdate.no-ip.com";
            
        }

        #region IDynamicDNSProvider Member

        public DynamicDNSUpdateResult UpdateIP(IPAddress ip, string yourdomainname, string username, string password)
        {
            BuildRequestString(ip, yourdomainname, username, password);
            return base.UpdateDNSEntry();
             
        }

        /// <summary>
        /// This is a helper method for replacing the template values against the correct values
        /// </summary>
        /// <param name="ip">Contains the IP Adress for which the Update is to processed</param>
        /// <param name="yourdomainname">Contains the Hostname for which the Update is to processed. This is Provider specific.</param>
        /// <param name="username">Contains the required username for the update</param>
        /// <param name="password">Contains the required password for the update</param>
        private void BuildRequestString(IPAddress ip, string yourdomainname, string username, string password)
        {
            Request = String.Format(REQUESTTEMPLATE, yourdomainname, ip.ToString(), System.Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(username + ":" + password)));
        }
        #endregion
    }
}
